package com.fjec.info.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjec.common.protocol.JsonResult;
import com.fjec.common.view.InfoDetailVO;
import com.fjec.common.view.InfoInformationVO;
import com.fjec.common.view.RpcReturnPage;
import org.springframework.web.bind.annotation.*;

import com.fjec.info.service.IInfoInformationService;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 025.资讯表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-12-12
 */
@RestController
@RequestMapping("/")
public class InfoInformationController {

    @Resource
    private IInfoInformationService service;

    /**
     * 上传图片（不保存附件表）
     *
     * @param file
     * @return
     */
    @PostMapping("/y/upload/rich/text/pic")
    public JsonResult uploadRichTextPic(@RequestParam("file") MultipartFile file) throws Exception {
        return service.uploadRichTextPic(file);
    }

    /**
     * 小程序根据Id获取资讯
     *
     * @param id
     * @return
     */
    @GetMapping("/n/info/get/detail")
    public JsonResult getInfoDetail(Long id) throws Exception {
        return service.getInfoDetail(id);
    }

    /**
     * 小程序分页查询资讯
     *
     * @param page     分页
     * @param category 类别 1=草莓资讯 2=首页轮播图
     * @return
     */
    @GetMapping("/n/info/get/page")
    public JsonResult getInformationPage(Page page,
                                         @RequestParam Integer category) throws Exception {
        return service.getInformationPage(page, category);
    }

    /**
     * 分页查询资讯
     *
     * @param current
     * @param size
     * @param category
     * @param title
     * @param startTime
     * @param endTime
     * @param getTop
     * @return
     */
    @GetMapping("/info/get/information/page")
    public RpcReturnPage<InfoInformationVO> rpcGetInformationPage(@RequestParam long current,
                                                                  @RequestParam long size,
                                                                  @RequestParam(required = false) Integer category,
                                                                  @RequestParam(required = false) String title,
                                                                  @RequestParam(required = false) String startTime,
                                                                  @RequestParam(required = false) String endTime,
                                                                  @RequestParam(required = false) Integer getTop) throws Exception {
        Page page = new Page(current, size);
        return service.rpcGetInformationPage(page, title, startTime, endTime, getTop, category);
    }

    /**
     * 保存或修改资讯
     *
     * @param category       暂时不用传
     * @param id             资讯Id
     * @param title          标题
     * @param summary        简介
     * @param coverPic       封面url
     * @param articleContent 富文本内容
     * @param display        是否显示 0=不显示 1=显示
     * @param getTop         是否置顶 0=未置顶 1=已置顶
     * @param space          两边留白
     * @return
     */
    @PostMapping("/info/save/information")
    public boolean rpcSaveOrUpdateInformation(@RequestParam(required = false) Integer category,
                                              @RequestParam(required = false) Long id,
                                              @RequestParam(required = false) String title,
                                              @RequestParam(required = false) String summary,
                                              @RequestParam(required = false) String coverPic,
                                              @RequestParam String articleContent,
                                              @RequestParam Integer display,
                                              @RequestParam(required = false) Integer getTop,
                                              @RequestParam(required = false) Integer space) {
        category = category == null || category < 1 ? 1 : category;
        getTop = getTop == null ? 0 : getTop;
        space = space == null ? 20 : space;
        return service.rpcSaveOrUpdateInformation(category, id, title, summary, coverPic, articleContent, display, getTop, space);
    }

    /**
     * 删除资讯
     *
     * @param id
     * @return
     */
    @PostMapping("/info/del/information")
    public boolean rpcLogicDelInformation(@RequestParam Long id) {
        return service.rpcLogicDelInformation(id);
    }

    /**
     * 根据Id获取资讯
     *
     * @param id
     * @return
     */
    @GetMapping("/info/get/by/id")
    public InfoDetailVO rpcGetInfoDetail(@RequestParam Long id) throws Exception {
        return service.rpcGetInfoDetail(id);
    }

}
